package com.campus.client.controller;

import com.campus.client.net.ClientConnector;
import com.campus.common.message.TeacherCourseRequest;
import com.campus.common.message.TeacherCourseResponse;
import com.campus.common.vo.Course;
import com.campus.common.vo.Student;

import java.io.IOException;
import java.util.List;

public class TeacherCourseController {
    private ClientConnector connector;

    public TeacherCourseController(ClientConnector connector) {
        this.connector = connector;
    }

    // 查询所有课程
    public List<Course> viewAllCourses() throws IOException, ClassNotFoundException {
        TeacherCourseRequest req = new TeacherCourseRequest();
        req.setAction(TeacherCourseRequest.Action.VIEW_ALL_COURSES);

        TeacherCourseResponse resp = connector.sendTeacherCourseRequest(req);
        return resp.getCourses();
    }

    // 查看当前教师教授的课程
    public List<Course> viewMyCourses(String teacherId) throws IOException, ClassNotFoundException {
        TeacherCourseRequest req = new TeacherCourseRequest();
        req.setAction(TeacherCourseRequest.Action.VIEW_TEACHING_COURSES);
        req.setTeacherId(teacherId);

        TeacherCourseResponse resp = connector.sendTeacherCourseRequest(req);
        return resp.getCourses();
    }

    // 按课程编号或名称查询
    public List<Course> queryCourses(String keyword) throws IOException, ClassNotFoundException {
        TeacherCourseRequest req = new TeacherCourseRequest();
        req.setAction(TeacherCourseRequest.Action.QUERY_COURSE);
        req.setCourseId(keyword);
        req.setCourseName(keyword);

        TeacherCourseResponse resp = connector.sendTeacherCourseRequest(req);
        return resp.getCourses();
    }

    // 查看某个班级的学生名单
    public List<Student> viewStudentsInClass(String classId) throws IOException, ClassNotFoundException {
        TeacherCourseRequest req = new TeacherCourseRequest();
        req.setAction(TeacherCourseRequest.Action.VIEW_STUDENTS_IN_CLASS);
        req.setClassId(classId);

        TeacherCourseResponse resp = connector.sendTeacherCourseRequest(req);
        return resp.getStudents(); // 学生对象列表
    }

    // 可选：关闭连接
    public void closeConnection() throws IOException {
        connector.close();
    }
}
